var sourcedata;
var sourcedowloadIndex = new Object();
var sourceSearchDataType="";
var pageSize = 10;
var pageNo = 1;
var source = {
	getDownHy:function(){
		var config = {
				url:'/api/showhy/download',
				data:{},
				callBackFunc:function(data){
					if(data != null){
						$(".ppm-form-input img[name='sign']").attr("src","data:image/png;base64,"+data.dataDownloadAgreementUimg);
						$(".ppm-form-input input[name='role']").attr("value",data.dataDownloadAgreementJob);
						$(".ppm-form-input input[name='date']").attr("value",data.dataDownloadAgreementDate);
					}
				},
				callBackErrorFunc:function(){}
		};
		HD.doPost(config);//提交
	},
	serarchPageDoc:function(data){
		var config = {
				url:'/sourceDoc',
				data:{},
				obj:"#sourceBox",
				callBackFunc:function(){
					$("#sourceBox").removeClass("ppm-list n-box");
					$("#sourceBox").addClass("ppm-form n-box");
					source.getDownHy();
				},
				callBackErrorFunc:function(){}
		};
		HD.loadAsy(config);
	},
	search:function(data){
		var jsonpage = new Object();
		jsonpage.pageSize = pageSize;
		jsonpage.pageNo = pageNo;
		ui.popProcess();
		var config = {
				url:'/api/search/SOURCE/'+data,
				data:{
					jsonpage:JSON.stringify(jsonpage)
				},
				callBackFunc:function(data){
					var res = data.data.result;
					if(res != null && res.length > 0){
						sourcedata = res;
					}else{
						layer.msg("NO DATA !");
						$(".n-nav-links a").eq(3).click();
						ui.shutProcess();
						return;
					}
					//组装数据前台显式
					$("#sourceBox").html(data.data.html);
					$(".n-page button").removeClass("n-page-cur");
					if(data.data.maxPageNo == pageNo){
						if(pageNo <= 3){
							$(".n-page button").eq(pageNo-1).addClass("n-page-cur");
						}else{
							$(".n-page button").eq(3).addClass("n-page-cur");
						}
					}else if(pageNo <= 3){
						$(".n-page button").eq(pageNo-1).addClass("n-page-cur");
					}else{
						$(".n-page button").eq(2).addClass("n-page-cur");
					}
					//选中
					source.checkedTD();
					ui.shutProcess();
				},
				callBackErrorFunc:function(){}
		};
		HD.doPost(config);//提交
	},
	checkTD:function(obj){
		var id = $(obj).parents("tr").attr("id");
		if($(obj).hasClass("n-checkboxCheck")){//取消
			$(obj).removeClass("n-checkboxCheck");
			delete sourcedowloadIndex[pageNo+"_"+id];
		}else{//选中
			$(obj).addClass("n-checkboxCheck");
			sourcedowloadIndex[pageNo+"_"+id] = sourcedata[id];
		}
	},
	checkedTD:function(){
		if(Object.keys(sourcedowloadIndex).length === 0){
			return;
		}else{
			for(var i in sourcedowloadIndex){
				var no = i.split("_")[0];
				var index = i.split("_")[1];
				if(pageNo == no){
					$("#sourcetbody tr").eq(index).find("span").addClass("n-checkboxCheck");
				}
			}
		}
	},
	addToserarchPage:function(){
		//请求页面
		source.search(sourceSearchDataType);
	},
	getItemLight : function(){
		var config = {
				url:'/api/pte/SOURCE',
				data:{},
				callBackFunc:function(data){
					//高亮显式ppm-tb-itemCur
					var items = $("#sourceBox li");
					if(data.data.length > 0){
						for(var x = 0 ; x < data.data.length;x++){
							for(var y = 0; y < items.length;y++){
								var itemType = (data.data[x].content).split("-")[0];
								var itemHtm = $(items[y]).find("h3").text();
								if(!$(items[y]).hasClass("ppm-tb-itemCur") && itemType == itemHtm){
									$(items[y]).addClass("ppm-tb-itemCur");
									continue;
								}
							}
						}
					}
				},
				callBackErrorFunc:function(){}
		};
		HD.doPost(config);//提交
	},
	//更改页码后，请求数据
	changePageNo:function(no){
		if(no != null && no >= 1){
			pageNo = Number(no);
			source.search(sourceSearchDataType);
		}
	},
	changeNextPageNo:function(obj,total){
		if(pageNo >= total){
			layer.msg("This is the last page!");
			return;
		}
		pageNo = Number(pageNo) + 1;
		source.changePageNo(pageNo);
	}
}

$(function(){
	//搜素
	$("#sourceSerarch").click(function(){
		var data = $(this).prev().val();
		if(data == null || data == undefined || data == ''){
			layer.msg("Please fill in the element name !");
			return;
		}
		sourceSearchDataType = data;
		$("#sourceBox").removeClass("ppm-tb n-box");
		$("#sourceBox").addClass("ppm-list n-box");
		pageNo = 1;
		sourcedowloadIndex = new Object();
		source.search(data);//转大写
	})
	//特定元素查询
	$(".ppm-search footer i").each(function(n,m){
		$(m).click(function(){
			var data = $(m).text();
			$(".ppm-search input[name='source']").attr("value",data);
			sourceSearchDataType = data;
			pageNo = 1;
			sourcedowloadIndex = new Object();
			source.search(data);//转大写
		})
	})
	//请求元素类型，默认高亮显式ppm-tb-itemCur
	source.getItemLight();
	
	//元素周期表点击事件绑定
	$("#sourceBox li").each(function(n,m){
		$(m).click(function(){
			if($(m).hasClass("ppm-tb-itemCur")){
				var data = $(m).find("h3").text();
				sourceSearchDataType = data;
				pageNo = 1;
				sourcedowloadIndex = new Object();
				source.search(data);
			}else{
				layer.msg("NO DATA !");
			}
		})
	})
})